﻿using DataAccess;
using KhoaLuan.Utils;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.ProcessAJAX
{
    public partial class AjaxFunction : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static string GetListMenu(int currentpage, int numberinpage, string keyword)
        {

            string link = "~/MainSys/Manager/Controls/ctManagementMenu.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.View))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xem danh sách các chức năng" });
            try
            {
                tbMenu_Proxy pr = new tbMenu_Proxy();
                List<tbMenu> list = new List<tbMenu>();
                int totalitem = 0;
                list = pr.SelectAlltbMenu_Proxy();
                if (keyword != "")
                {
                    keyword = keyword.ToLower();
                    list = list.FindAll(x => x.MenuName.ToLower().Contains(keyword) || (x.Link != null && x.Link.ToLower().Contains(keyword)));
                }
                totalitem = list.Count;
                var list2 = list.Skip((currentpage - 1) * numberinpage).Take(numberinpage);
                return new JavaScriptSerializer().Serialize(new { Status = "Success", Data = list2, TotalItem = totalitem, CurrentPage = currentpage });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error", Data = "" });
            }
        }
        [WebMethod]
        public static string DeleteMenu(int Id)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementMenu.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Delete))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xóa các chức năng" });
            try
            {
                tbMenu_Proxy pr = new tbMenu_Proxy();
                if (pr.Delete_tbMenu_Proxy(Id))
                    return new JavaScriptSerializer().Serialize(new { Status = "Success" });
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }
        [WebMethod]
        public static string InsertMenu(int ParentID, string MenuName, string Description, string HTML, int OrderID, string Link, int OrgID)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementMenu.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.AddNew))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền thêm mới chức năng" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbMenu).Name);
                tbMenu_Proxy pr = new tbMenu_Proxy();
                DataAccess.tbMenu a = new DataAccess.tbMenu();
                a.MenuID = 0;
                a.ParentID = ParentID;
                a.MenuName = MenuName;
                a.Description = Description;
                a.HTML = HTML;
                a.OrderID = OrderID;
                a.Link = Link;
                a.OrgID = OrgID;
                if (pr.Insert_tbMenu_Proxy(a))
                    log.Info(UserLogin.GetUserName + "Thêm mới Menu thành công");
                return new JavaScriptSerializer().Serialize(new { Status = "Success" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }
        [WebMethod]
        public static string UpdateMenu(int MenuID, int ParentID, string MenuName, string Description, string HTML, int OrderID, string Link, int OrgID)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementMenu.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Edit))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền chỉnh sửa chức năng" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbMenu).Name);
                tbMenu_Proxy pr = new tbMenu_Proxy();
                DataAccess.tbMenu a = new DataAccess.tbMenu();
                a.MenuID = 0;
                a.ParentID = ParentID;
                a.MenuName = MenuName;
                a.Description = Description;
                a.HTML = HTML;
                a.OrderID = OrderID;
                a.Link = Link;
                a.OrgID = OrgID;
                if (pr.Update_tbMenu_Proxy(a))
                    log.Info(UserLogin.GetUserName + "Cập nhật Menu thành công");
                return new JavaScriptSerializer().Serialize(new { Status = "Success" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }
    }
}